﻿// (c) Copyright Fernando Cerqueira.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;

// ReSharper disable CheckNamespace
namespace WindowsPhoneCommands
// ReSharper restore CheckNamespace
{
    public static class BingTranslate
    {
        public static string TranslateText(string text, string from, string to, out string error)
        {
            var qtdspaceend = text.Length - text.TrimEnd().Length;
            var qtdspaceini = text.Length - text.TrimStart().Length;
            string result = null;
            string err = null;

            var uri = "https://api.datamarket.azure.com/Data.ashx/Bing/MicrosoftTranslator/v1/Translate?Text=%27" + text + "%27&To=%27" + to + "%27&From=%27" + from + "%27&$top=1&$format=Raw";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Credentials = new NetworkCredential(
                EncryptDecryptMd5.DecryptString(UserSettings.Current.BingClientId, EncryptDecryptMd5.DecryptString(UserSettings.Current.Md5Password, Constants.GuidRef)),
                EncryptDecryptMd5.DecryptString(UserSettings.Current.BingSecretCode, EncryptDecryptMd5.DecryptString(UserSettings.Current.Md5Password, Constants.GuidRef)));
            httpWebRequest.Proxy = UserSettings.Current.Proxy;
            WebResponse response = null;
            try
            {
                response = httpWebRequest.GetResponse();
                // ReSharper disable AssignNullToNotNullAttribute
                using (var stream = response.GetResponseStream())
                {
                    var dcs = new DataContractSerializer(Type.GetType("System.String"));
                    result = (string)dcs.ReadObject(stream);
                    result = result + new string(' ', qtdspaceend);
                    result = result + new string(' ', qtdspaceini);
                }
                // ReSharper restore AssignNullToNotNullAttribute
            }
            catch (WebException e)
            {
                string strResponse;
                using (response = e.Response)
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        // ReSharper disable AssignNullToNotNullAttribute
                        using (var sr = new StreamReader(responseStream, System.Text.Encoding.ASCII))
                        // ReSharper restore AssignNullToNotNullAttribute
                        {
                            strResponse = sr.ReadToEnd();
                        }
                    }
                }
                err = strResponse;
            }
            catch (Exception ex)
            {
                err = ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            error = err;
            return result;
        }
    }

}
